package tools;
/* FixedLengthDocument.java */

import javax.swing.text.*;
   
public class NewDocument extends PlainDocument
{
	private int iMaxLength;
	private boolean onlyNumber = false;
 
	public NewDocument(int maxlen) {
		super();
		iMaxLength = maxlen;
	}
	
	public NewDocument(int maxlen, boolean somenteNumeros ) {
		super();
		iMaxLength = maxlen;
		onlyNumber = somenteNumeros;
	}

	public void insertString(int offset, String str, AttributeSet attr)
					throws BadLocationException	{
		if (str == null) 
			return;
		
		if (onlyNumber == true)
			if(Character.isDigit(str.charAt(0)) == false)
				return;

		if (iMaxLength <= 0)        // aceitara qualquer no. de caracteres
		{
			super.insertString(offset, str, attr);
			return;
		}

		int ilen = (getLength() + str.length());
		if (ilen <= iMaxLength)    // se o comprimento final for menor...
			super.insertString(offset, str, attr);   // ...aceita str
		}
}
